<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Child Page</title>
     <style>
        .box {
            background: #cccce5;
            padding: 10px;
            margin: 20px;
        }
    </style>
</head>
<body>
    <h3>测试 window.open() 打开的窗口和父窗口的通信方式</h3>

    <div class="box">
        <button id="sendMessage2">用window发送消息给父窗口</button>
        <div id="message">
            <h4>child win recieve messages:</h4>
        </div>

    </div>

    <div class="box">
        <h3>opener listen message event and recieve message from parent-win</h3>
    </div>

    <div class="box">
        <button id="sendMessage">用window.opener发送消息给父窗口</button>

    </div>

    <div class="box">
        <button onclick="writeStorage()">write storage</button>
        <p>cross domain, take no effect</p>
    </div>

    <div class="box">
        <button onclick="backToParent()">激活父窗口</button>
        <p>window.opener.focus()  not works</p>
    </div>

    <div class="box">
        <button onclick="backToParent2()">激活父窗口2</button>
        <p>opener.external.comeback()</p>
        <p>cros error</p>
    </div>

    <div class="box">
        <button onclick="closeMe()">关闭子窗口</button>
        <p>window.close() success</p>
        <p>if not change to other tabs, will focus on parent window</p>
        <p>if used to change to other tabs, will focus on the right tab</p>
    </div>


    <script>
        function backToParent2() {
            console.log('backToParent2 called..');
            window.opener.external.comeback();
        }

        function closeMe() {
            window.close();
        }

        function backToParent() {
            console.log('backToParent called..');
            setTimeout(() => {
                window.opener.focus();

            }, 100);

        }

        function outputMsg(msg, id='message') {
            const p = document.createElement('p');
            p.innerText = msg;
            document.getElementById(id).appendChild(p);
        }
        
        window.favcolor = 'blue'

        const sendBtn = document.getElementById('sendMessage');
        sendBtn.onclick = function() {
            window.opener.postMessage('opener say: Hello from child window!', '*');
        }

        const sendBtn2 = document.getElementById('sendMessage2');
        sendBtn2.onclick = function() {
            console.log(1)
            window.postMessage('child say: Hello from child window!', '*');
            console.log(2)
        }


        window.addEventListener('message', function(event) {
            console.log(event.data, typeof event.data);
            outputMsg(event.data);
        }, false);


        // corss origin, access addEventListener error
        // window.opener.addEventListener('message', function(event) {
        //     console.log(event.data);
        //     document.getElementById('message').innerText = event.data;
        // }, false);


        function writeStorage() {

            window.localStorage.setItem('test', 'test' + Date.now());
        }

    </script>
</body>
</html>